package com.clean.cleantotal.compores.large

import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseGroupActivity
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.databinding.ActivityLargeBinding
import com.clean.cleantotal.utils.FormatterSize
import com.clean.cleantotal.utils.Utils
import com.drake.brv.utils.bindingAdapter

class LargeActivity : BaseGroupActivity<ActivityLargeBinding>() {
    override val viewBinding: ActivityLargeBinding
        get() = ActivityLargeBinding.inflate(layoutInflater)

    override fun initView() {
        super.initView()
        mBinding.back.setOnClickListener { onBackPressed() }
        mBinding.tvClean.text =
            "Clean (${FormatterSize.formatFileSize(this, selectList.sumOf { it.size })})"
        Utils.showScanDialog(this, FunType.LARGE, mBinding.loadingLayout.scanLayout, {
            animal = it
        }) {

        }
        scanJunkUtils?.scanLargeFiles(lifecycleScope, this) {
            initRecyclerViewData(it)
            mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
            animal?.duration = 3000
            mBinding.tvClean.text =
                "Clean (${FormatterSize.formatFileSize(this, selectList.sumOf { it.size })})"
            mBinding.includedLayout.ivFinish.isVisible = false
            mBinding.includedLayout.tvTitle.text = "Found"
            val size =
                FormatterSize.formatFileSizeSplit(this, totalSize)
            mBinding.includedLayout.tvContent.text =
                "Files larger than ${size.first()}${size.last()} will be \n displayed here."
            mBinding.includedLayout.tvSize.text = size.first()
            mBinding.includedLayout.tvUnit.text = size.last()
        }
    }

    override fun isLargeScan(): Boolean {
        return true
    }

    override fun initGroupRecyclerView(): RecyclerView {
        return mBinding.recyclerView
    }

    override fun allSelectView(): View {
        return mBinding.allLayout
    }

    override fun clickCleanView(): View {
        return mBinding.tvClean
    }

    override fun updateUi() {
        isAllSelect = selectList.size == totalCount
        mBinding.ivAll.setImageResource(if (isAllSelect) R.mipmap.green_clean_hospital else R.mipmap.green_clean_careful)

        mBinding.tvClean.text = "Clean (${
            FormatterSize.formatFileSize(this, selectList.sumOf { it.size })
        })"
        mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
    }
}